package net.optix.ip.commands
{
	import com.adobe.cairngorm.business.Responder;
	import com.adobe.cairngorm.commands.Command;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.adobe.cairngorm.control.CairngormEventDispatcher;
	
	import mx.controls.Alert;
	
	import net.optix.ip.business.*;
	import net.optix.ip.events.GetCopyrightsEvent;
	import net.optix.ip.events.SetCopyrightEvent;
	import net.optix.ip.model.ModelLocator;
	
	public class SetCopyrightCommand implements Command, Responder
	{
		// attributes ============================
		private var model : ModelLocator = ModelLocator.getInstance();


		// functions ============================
		public function execute( event:CairngormEvent ) : void
		{
			var myEvent:SetCopyrightEvent = SetCopyrightEvent( event );
			
			var delegate:CopyrightDelegate = new CopyrightDelegate();     
			delegate.setCopyright( this,myEvent.vo );
			
		}
		
		//----------------------------------------------------------------------------
		public function onResult( event : * = null ) : void
		{
			trace(event.result);
			var title:String	=	"Result of save copyright";
			var message:String;
			var result:int;
			try
			{
				result				=	event.result.result;
				switch(result)
				{
					case -2:
						message	=	"Access Denied.";
						break;
					case 0:
						message	=	"Program error!";
						break;
					case 1:
						message	=	"Successfully recorded copyright.";				
						break;
				}
			}
			catch(error:Error)
			{
				
				message	=	"Program error";
			}
			
			model.bSubmit	=	true;
			var alert:Alert	=	 
					Alert.show(message,title,Alert.OK, null,null, null,  Alert.OK );
			alert.width		=	360;
			alert.height	=	130;
			
			if(result == 1)
			{
				var userID:int;
				try
				{
					switch(model.role)
					{
						case 1:
							userID	=	model.client.clientID;
							break;
						case 2:
							userID	=	model.inventor.inventorID;
							break;
						case 3:
							userID	=	model.firm.firmID;
							break;
						case 4:
							userID	=	model.practitioner.practitionerID;
							break;
					}
					var getCopyriths:GetCopyrightsEvent	=	new GetCopyrightsEvent(userID,model.role);
					CairngormEventDispatcher.getInstance().dispatchEvent(getCopyriths);
				}
				catch(error:Error){}
			}
		}
		
		//----------------------------------------------------------------------------
		public function onFault( event : * = null ) : void
		{
			model.result	=	"Program error";	
			model.bSubmit	=	true;
		}

	}
}